$(document).ready(function() {
  require(["inputdrop", "tree"], function(inputdrop, tree) {
    var list_col = [
      { text: "", name: "t", width: 3, hideSearch: "hide" },
      { text: "资产名称", name: "deviceName", align: "left", width: 20 },
      {
        text: "资产标识",
        name: "deviceId",
        align: "left",
        width: 20
      },
      {
        text: "IP地址",
        name: "ip",
        align: "left",
        width: 20
      },
      {
        text: "物理地址",
        name: "mac",
        align: "left",
        width: 20
      },
      {
        text: "部署网络",
        name: "networkName",
        align: "left",
        width: 20
      },
      {
        text: "管控域名称",
        name: "systemName",
        align: "left",
        width: 20,
        tip:true
      }
    ];

    var current_selected_node_id = "";

    var key;
    event_init();
    $("input[placeholder]").placeholder();
    event_class_tree();
    asset_type_list({
      paramObj: { pageSize: 50 },
      isLoad: true,
      maskObj: "body"
    });
    function view_init() {
      $("#content_div").css("padding", "0");
      $("#pg-content").css("padding", "0");
    }

    function event_init() {
      $("#add_type_btn").click(function() {
        open_asset_type_dialog();
      });
      $("#add_btn").click(function() {
        open_asset_class_dialog();
      });

      $("#searchIcon").click(function() {
        key = $("#searchKey").val();

        g_grid.refresh($("#table_div1"), {
            paramObj: {keyword: key}
          });
        // asset_type_list({
        //   paramObj: {
        //     keyword: key,
        //     categoryId: current_selected_node_id
        //   },
        //   isLoad: true,
        //   maskObj: "body"
        // });
      });
      $("#searchKey").keydown(function(event) {
        if (event.keyCode == 13) {
          $("#searchIcon").trigger("click");
        }
      });
    }

    function event_class_tree(opt) {
      if (opt) {
        current_selected_node_id = opt;
      }
      xd_ajax_get({
        url: "api/property/countByType",
        paramObj: {categoryId:1},
        isLoad: false,
        successCallBack: function(data) {
          data = data.reverse()
          tree.simpleTree($("#event_class_tree"), {
            zNodes: data,
            zTreeOnClick: function(treeNode) {
              current_selected_node_id = treeNode.id;
              // current_selected_node_name = treeNode.label;
              var paramObj={}
              if(treeNode.id==1){
              }else{
                 paramObj = { typeId: treeNode.id };
              }
              if (treeNode.id == "-1") {
                paramObj = null;
              }
              asset_type_list({
                paramObj: paramObj,
                isLoad: true,
                maskObj: "body"
              });
            },
            initIdVal: "-1"
          });
        },
        
      });
    }

    function asset_type_list(option) {
      g_grid.render($("#table_div1"), {
        header: list_col,
        url: "api/public/agent/list",
        type: "xd",
        dataKey: "list",
        operWidth: "60px",
        paramObj: option.paramObj,
        queryBefore: function(paramObj) {
          if (current_selected_node_id != -1) {
            paramObj.categoryId = 1;
          }
        },
        isLoad: option.isLoad,
        maskObj: option.maskObj,
        dbClick: detailFunc,
        dbIndex: 1,
        hasBorder: false,
        hideSearch: true,
        autoHeight: true,
        showCount: true,
        allowCheckBox: false
      });
    }

    
  

 

    function detailFunc(rowData) {
      window.location.hash =
        "#/exclusive/exclusive_hash_detail?id=" + rowData.deviceId;
    }



    xd_ajax_get({
      url: "api/dic/getDicList",
      paramObj: { parentCode: "SYS_NETWORK_CODE" },
      isLoad: true,
      successCallBack: function(data) {
        var dataList = [];
        data ? (dataList = []) : (data = []);
        data.forEach(function(item) {
          dataList.push({ value: item.code, text: item.name });
        }, this);

        g_formel.list_filter_render($("um-filter"), {
          condition: [
            {
              text: "部署网络",
              name: "networkCode",
              data: dataList
            }
          ],
          click: function(paramObj) {
            // paramObj.networkCode=paramObj.networkCode.split(",")
            g_grid.refresh($("#table_div1"), {
              paramObj: paramObj
            });
          }
        });
    
      }
    });
  
  });
});
